package com.demo.business.store;

import com.tiangong.framework.annotation.OpenApi;
import com.tiangong.framework.bo.CDO;
import com.tiangong.framework.bo.Return;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description: 3）门店表
 * @create: 2022-02-25 23:30:08
 **/
@Service("store")
@Slf4j
public class StoreService{

    @Resource
    StoreDao storeDao;

    /**
     * 添加3）门店表,获取lStoreId数据
     *
     * @param cdoRequest - lStoreId:
     *                   - sStoreName:门店名称
     *                   - sSynopsis:门店介绍
     *                   - sImgUrl:门店图路径
     *                   - sTelephone:门店电话
     *                   - sCityName:城市
     *                   - sAreaName:区
     *                   - sAddress:门店地址
     *                   - nLat:纬度
     *                   - nLng:经度
     *                   - sSubscribe:订阅标识
     *                   - sUsablearea:使用面积
     *                   - ibed:床位数
     *                   - iState:门店状态：0、开业中；1、已关门
     *                   - iParking:是否停车：0、是 ，1、否
     *                   - dCreateTime:创建时间
     *                   - dUpdateTime:更新时间
     * @return
     * @author 
     * @create: 2022-02-25 23:30:08
     */
    @OpenApi
    public Return addStore(CDO cdoRequest) {
        storeDao.addStore(cdoRequest);
        CDO cdoResult = new CDO();
        if (cdoRequest.containsKey("lStoreId")) {
            cdoResult.set("lStoreId", cdoRequest.get("lStoreId"));
        }
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据lStoreId，更新3）门店表数据
     *
     * @param cdoRequest - lStoreId:
     *                   - sStoreName:门店名称
     *                   - sSynopsis:门店介绍
     *                   - sImgUrl:门店图路径
     *                   - sTelephone:门店电话
     *                   - sCityName:城市
     *                   - sAreaName:区
     *                   - sAddress:门店地址
     *                   - nLat:纬度
     *                   - nLng:经度
     *                   - sSubscribe:订阅标识
     *                   - sUsablearea:使用面积
     *                   - ibed:床位数
     *                   - iState:门店状态：0、开业中；1、已关门
     *                   - iParking:是否停车：0、是 ，1、否
     *                   - dCreateTime:创建时间
     *                   - dUpdateTime:更新时间
     * @return
     * @author 
     * @create: 2022-02-25 23:30:08
     */
    @OpenApi
    public Return updateStoreById(CDO cdoRequest) {
        int affectedRows = storeDao.updateStoreById(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据lStoreId，删除3）门店表数据
     *
     * @param cdoRequest - lStoreId:
     * @return
     * @author 
     * @create: 2022-02-25 23:30:08
     */
    @OpenApi
    public Return deleteStoreById(CDO cdoRequest){
        int affectedRows = storeDao.deleteStoreById(cdoRequest.getLong("lStoreId"));
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据条件，删除3）门店表数据
     *
     * @param cdoRequest
     * @return
     * @author 
     * @create: 2022-02-25 23:30:08
     */
    //@OpenApi
    public Return deleteStore(CDO cdoRequest) {
        int affectedRows = storeDao.deleteStore(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据ID，获取3）门店表数据
     *
     * @param cdoRequest - lStoreId:
     * @return
     * @author 
     * @create: 2022-02-25 23:30:08
     */
    @OpenApi
    public Return getStoreById(CDO cdoRequest) {
        CDO cdoMapperResult = storeDao.getStoreById(cdoRequest.getLong("lStoreId"));
        return Return.valueOf(cdoMapperResult);
    }

    /**
     * 根据混合条件，获取3）门店表数据
     *
     * @param cdoRequest - lStoreId:
     * @return
     * @author 
     * @create: 2022-02-25 23:30:08
     */
    @OpenApi
    public Return getStore(CDO cdoRequest) {
        CDO cdoMapperResult = storeDao.getStore(cdoRequest);
        return Return.valueOf(cdoMapperResult);
    }

    /**
     * 根据非空条件，获取3）门店表数据列表
     *
     * @param cdoRequest
     * @return
     * @author 
     * @create: 2022-02-25 23:30:08
     */
    @OpenApi
    public Return getStoreList(CDO cdoRequest) {
        Integer pageIndex = cdoRequest.containsKey("pageIndex") ? cdoRequest.getInteger("pageIndex") : 0;
        int recordSize = cdoRequest.containsKey("recordSize") ? cdoRequest.getInteger("recordSize") : 10;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        List<CDO> cdoMapperResultList = storeDao.getStoreList(cdoRequest);
        boolean isEndPage = false;
        if (cdoMapperResultList.size() < recordSize) {
            isEndPage = true;
        }
        CDO cdoResult = new CDO();
        cdoResult.set("isEndPage", isEndPage);
        cdoResult.set("datalist", cdoMapperResultList);
        int total = storeDao.getStoreListCount(cdoRequest);
        cdoResult.set("total", total);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据混合条件，获取3）门店表数据列表
     *
     * @param cdoRequest - lStoreId:
     * @return
     * @author 
     * @create: 2022-02-25 23:30:08
     */
    @OpenApi
    public Return getStoreListByCondition(CDO cdoRequest) {
        Integer pageIndex = cdoRequest.containsKey("pageIndex") ? cdoRequest.getInteger("pageIndex") : 0;
        int recordSize = cdoRequest.containsKey("recordSize") ? cdoRequest.getInteger("recordSize") : 10;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        List<CDO> cdoMapperResultList = storeDao.getStoreListByCondition(cdoRequest);
        boolean isEndPage = false;
        if (cdoMapperResultList.size() < recordSize) {
            isEndPage = true;
        }
        CDO cdoResult = new CDO();
        cdoResult.set("isEndPage", isEndPage);
        cdoResult.set("datalist", cdoMapperResultList);
//        int total = storeDao.getStoreListByConditionCount(cdoRequest);
//        cdoResult.set("total", total);
        return Return.valueOf(cdoResult);
    }
}
